package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.SmallPageBean;
import com.itheima.exception.CheckItemDeleteFailException;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    //注入dao
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页显示几条数据
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();

        //分页助手插件
        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> list = checkItemDao.selectByCondition(queryString);

        //总条数
        long total = list.getTotal();
        //全部数据
        List<CheckItem> result = list.getResult();

        return new PageResult(total,result);
    }

    @Override
    public void deleteById(Integer id) {
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0){
            throw new CheckItemDeleteFailException();
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return null;
    }

    @Override
    public PageResult findAll(String checkGroupId,SmallPageBean smallPageBean) {
        //当前页
        Integer smallCurrentPage = smallPageBean.getSmallCurrentPage();
        //每页显示多少条
        Integer smallPageSize = smallPageBean.getSmallPageSize();
        //分页插件助手
        PageHelper.startPage(smallCurrentPage,smallPageSize);
        Page<CheckItem> list = checkItemDao.findAll(checkGroupId);
        //获取Page里面的总条数，和数据
        long total = list.getTotal();
        List<CheckItem> result = list.getResult();

        return new PageResult(total,result);
    }
}
